<template>
  <div>
    <my-header :showBack="true" title="CEC 兑换"></my-header>
    <div class="currency_conversion">
      <div class="currency_conversion_top">
        <ul class="cec_num">
          <li>
            <div>{{indexData.convert.convertTotalNum||0}}</div>
            <div>{{indexData.convert.convertRemark}} CEC 总量</div>
          </li>
          <li><span></span></li>
          <li>
            <div>{{indexData.convert.convertNum||0}}</div>
            <div>{{indexData.convert.convertRemark}} CEC 剩余</div>
          </li>
        </ul>
        <div class="cec_time"><div>{{indexData.convert.convertRemark}}时间：</div>{{indexData.convert.convertStartTime|formatDate}} ~ {{indexData.convert.convertEndTime-1000|formatDate}}</div>
        <ul class="my_num">
          <li>
            <div>{{Math.floor(indexData.useMoney*100)/100||0}}</div>
            <div>可用帮余额</div>
          </li>
          <li><span></span></li>
          <li>
            <div>{{Math.floor(indexData.useCoin*100)/100||0}}</div>
            <div>可用CEC</div>
          </li>
        </ul>
      </div>
      <div class="currency_conversion_box">
        <van-cell-group style="margin-top: 10px;">
          <van-field
            v-model="moneyRate"
            type="text"
            label="当日价格"
            disabled
          />
          <div class="van-cell van-hairline van-field">
            <div class="van-cell__title"><span>余额数量</span></div>
            <div class="van-cell__value">
              <input type="number" v-model="moneyNum" placeholder="请输入余额数量" @keyup="calculation" class="van-field__control">
            </div>
          </div>
          <div class="van-cell van-hairline van-field">
            <div class="van-cell__title"><span>CEC 数量</span></div>
            <div class="van-cell__value">
              <input type="number" v-model="payMoney" placeholder="请输入 CEC 数量" @keyup="calculation1" class="van-field__control">
            </div>
          </div>
          <van-field
            v-model="payPass"
            type="password"
            label="支付密码"
            placeholder="请输入支付密码"
          />
        </van-cell-group>
        <div class="transfer_accounts_tip">新用户默认支付密码为：123456，您可以在账户设置中更改</div>
        <van-button type="primary" class="conversion_btn" :disabled="disabledBtn" @click="comfirmBuy">确认兑换</van-button>
      
      </div>
      <div class="conversion_record">
        <p>兑换记录</p>
        <table>
          <tr>
            <th style="width:130px;">时间</th>
            <th>余额数量</th>
            <th>CEC 数量</th>
          </tr>
          <tr v-for="item in recordList">
            <td>{{item.createTime|formatDate}}</td>
            <td>{{item.convertMoney}}</td>
            <td>{{Math.floor(item.convertNum*100)/100}}</td>
          </tr>
        </table>
      </div>
    </div>
  </div>
</template>
<script>
import timeFormat from "../../store/timeFormat";
import header from "@/components/header";
import { Toast } from "vant";
export default {
  components: {
    "my-header": header
  },
  data() {
    return {
      moneyNum: "",
      payMoney: "",
      payPass: "",
      disabledBtn: false,
      ratioRate: 0,
      moneyRate: 0,

      loading: false,
      finished: true,
      recordList: [],
      searchData: {
        pageNo: 1, //当前页
        pageSize: 30, //每页30条
        type: 4
      },
      indexData: {
        useMoney: 0,
        useCoin: 0,
        convert: {
          convertNum: 0,
          convertStartTime: 0,
          convertEndTime: 0
        }
      }
    };
  },
  filters: {
    formatDate(time) {
      var date = new Date(time);
      return timeFormat.formatDate(date, "yyyy.MM.dd hh:mm:ss");
    },
    formatDate1(time) {
      var date = new Date(time);
      return timeFormat.formatDate(date, "yyyy.MM.dd");
    }
  },
  mounted() {
    this.init();
    this.selectIndexData();
  },
  methods: {
    selectIndexData() {
      this.$ajax(this.$api.selectIndexData).then(res => {
        if (res.code == 0) {
          this.indexData = res.data;
          this.ratioRate = res.data.ratioRate;
          this.moneyRate = res.data.moneyRate;
        }
      });
    },
    init() {
      this.searchData.pageNo = 1;
      this.$ajax(this.$api.selectAccountConvert, this.searchData).then(res => {
        this.loading = false;
        if (res.code === 0) {
          if (res.data && res.data.list.length > 0) {
            this.recordList = res.data.list;
            this.finished = this.searchData.pageNo == res.data.pageCount;
            this.loading = false;
          }
        } else {
          this.recordList = [];
        }
      });
    },
    loadMore: function() {
      this.searchData.pageNo += 1;
      this.$ajax(this.$api.selectAccountConvert, this.searchData).then(res => {
        this.loading = false;
        if (res.code === 0) {
          if (res.data && res.data.list.length > 0) {
            this.recordList = this.recordList.concat(res.data.list);
            this.finished = this.searchData.pageNo == res.data.pageCount;
            this.loading = false;
          }
        }
      });
    },
    calculation() {
      if (!isNaN(this.moneyNum) && this.moneyNum > 0) {
        this.payMoney =Math.floor(this.moneyNum * this.ratioRate * 100000) / 100000;
      } else {
        this.payMoney = "";
      }
    },
    calculation1() {
      if (!isNaN(this.payMoney) && this.payMoney > 0) {
        this.moneyNum = Math.floor(this.payMoney * this.moneyRate * 100000) /100000;
      } else {
        this.moneyNum = "";
      }
    },
    comfirmBuy() {
      if (isNaN(this.moneyNum)) {
        Toast({ message: this.$t('lang.EnterTrueNum'), position: "bottom" });
        return;
      } else if (this.moneyNum <= 0) {
        Toast({ message: "请输入大于0的数量", position: "bottom" });
        return;
      }
      // else if(this.moneyNum%50!=0){
      //   Toast({ message: "兑换数量必须是50的倍数", position: "bottom" });
      //   return;
      // }
      if (this.payPass == "") {
        Toast({ message: "请输入支付密码", position: "bottom" });
        return;
      }

      this.disabledBtn = true;
      this.$ajax(this.$api.doMoneyToCoin, {
        moneyNum: this.moneyNum,
        payPass: this.payPass
      }).then(res => {
        if (res.code == 0) {
          Toast({ message: "兑换成功", position: "bottom" });
          this.init();
          this.selectIndexData();
          this.moneyNum = "";
          this.payMoney = "";
          this.payPass = "";
        }
        this.disabledBtn = false;
      });
    }
  }
};
</script>

<style scoped>
.currency_conversion {
  background: #fff;
  font-size: 14px;
}
.currency_conversion_top {
  padding: 10px 0 5px;
  background: #25a3fe;
}
.cec_num {
  display: flex;
  line-height: 25px;
  color: #fff;
  padding: 20px 20px 0;
}
.cec_num li {
  max-width: 50%;
  flex: 1;
  text-align: center;
}
.cec_num li:nth-child(2) {
  max-width: 2px;
  flex: 1;
}
.cec_num span {
  display: block;
  height: 20px;
  width: 1px;
  background: #fff;
  margin-top: 15px;
}
.cec_num li div:first-child {
  font-size: 20px;
  font-weight: 700;
}
.cec_num li div:last-child {
  font-size: 12px;
}
.cec_time {
  text-align: center;
  padding: 10px 20px;
  color: #ffca79;
  font-size: 12px;
}
.my_num {
  display: flex;
  color: #fff;
  font-size: 12px;
  box-sizing: border-box;
  padding: 8px 20px;
  background: #65b4ec;
}

.my_num li {
  max-width: 50%;
  flex: 1;
  text-align: center;
  box-sizing: border-box;
}
.my_num li:nth-child(2) {
  max-width: 2px !important;
  flex: 1;
}
.my_num span {
  display: block;
  height: 20px;
  width: 1px;
  background: #fff;
  margin-top: 6px;
}
.transfer_accounts_tip {
  font-size: 12px;
  color: #d20000;
  margin: 5px 15px;
}
.currency_conversion_box {
  background: #fff;
}
.conversion_box {
  display: flex;
  padding: 15px 10px 0;
  align-items: center;
}
.conversion_box input {
  padding: 10px;
  margin: 0;
  width: 100%;
  font-size: 14px;
  border: none;
  background: #f4f4f4;
  border-radius: 0;
  height: auto;
  box-sizing: border-box;
  color: #888;
  text-align: center;
}
.conversion_box div {
  flex: 1;
}
.conversion_box div:nth-child(2) {
  max-width: 40px;
  text-align: center;
  font-size: 20px;
  color: #25a3fe;
}

.conversion_btn {
  width: 40%;
  margin: 10px 30%;
  text-align: center;
  background: #25a3fe;
  border: 1px solid #25a3fe;
  color: #fff;
  line-height: 38px;
  height: 38px;
  border-radius: 5px;
  display: inline-block;
}
.conversion_record {
  margin-top: 10px;
}
.conversion_record p {
  padding: 0 15px;
  line-height: 40px;
  background: #fff;
}
.conversion_record table {
  width: 100%;
  word-wrap: break-word;
  word-break: break-all;
  text-align: center;
  border-collapse: collapse;
  padding: 2px;
  border: 1px solid #f1f1f1;
  color: #333;
  margin-bottom: 10px;
}
.conversion_record tr {
  border: 1px solid #f1f1f1;
}
.conversion_record th {
  border: 1px solid #f1f1f1;
  padding: 10px 0;
  background: #fff;
  color: #666;
}
.conversion_record td {
  border: 1px solid #f1f1f1;
  padding: 10px 0;
  background: #fff;
  color: #888;
  font-size: 12px;
}
</style>
